package com.fjb.controller.brand;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fjb.common.entity.HttpCode;
import com.fjb.common.entity.JsonResult;
import com.fjb.common.enums.brand.BrandStatus;
import com.fjb.common.enums.product.CategoryStatus;
import com.fjb.common.global.GlobalConfig;
import com.fjb.pojo.brand.BrandInfo;
import com.fjb.pojo.brand.vo.BrandInfoVo;
import com.fjb.pojo.product.query.ProductCategoryQuery;
import com.fjb.pojo.product.vo.ProductCategoryVo;
import com.fjb.service.brand.BrandInfoService;
import com.fjb.service.product.ProductCategoryService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 品牌信息 前端控制器
 * </p>
 *
 * @author hemiao
 * @since 2019-12-22
 */
@Controller
@RequestMapping("/brand")
public class BrandInfoController {
	
	@Autowired
	private BrandInfoService brandInfoService;
	@Autowired
	private ProductCategoryService productCategoryService;
	
	/**
	 * @Description:查询有效的品牌列表
	 * @param request
	 * @return
	 * JsonResult<List<BrandInfoVo>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月28日 下午7:36:15
	 */
	@RequestMapping(value="/selectList")
	@ResponseBody
	public JsonResult<Map<String, Object>> selectList(HttpServletRequest request){
		HashedMap<String, Object> resultMap = new HashedMap<String, Object>();
		BrandInfo brandInfo = new BrandInfo();
		brandInfo.setBrandStatus(BrandStatus.STATUS_1.getCode());
		List<BrandInfoVo>  brandList = brandInfoService.selectBrandList(brandInfo);
		if(brandList.size()>0) {
			for (BrandInfoVo brandInfoVo : brandList) {
				String logo = brandInfoVo.getBrandLogo();
				if(StringUtils.isNoneBlank(logo)) {
					brandInfoVo.setBrandLogo(GlobalConfig.FILE_SERVICE_LOCATION+logo);
				}
			}
		}
		BrandInfoVo brandInfoVo = brandList.get(0);
		Integer brandId = brandInfoVo.getId();
		resultMap.put("currentBrandList", brandInfoVo);
		resultMap.put("brandList", brandList);
			
		ProductCategoryQuery categoryQuery = new ProductCategoryQuery();
		categoryQuery.setCategoryStatus(CategoryStatus.STATUS_1.getCode());
		categoryQuery.setBrandId(brandId);
		List<ProductCategoryVo> currentSubCategory = productCategoryService.selectList(categoryQuery);
		if(currentSubCategory.size()>0) {
			for (ProductCategoryVo pc : currentSubCategory) {
				String appletIcon = pc.getAppletIcon();
				if(StringUtils.isNoneBlank(appletIcon)) {
					pc.setAppletIcon(GlobalConfig.FILE_SERVICE_LOCATION+appletIcon);
				}	
			}
		}
		resultMap.put("currentSubCategory", currentSubCategory);
		return new JsonResult<Map<String, Object>>(resultMap, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:获得品牌和分类列表
	 * @param request
	 * @return
	 * JsonResult<List<BrandInfoVo>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月15日 下午10:09:55
	 */
	@RequestMapping(value="/getBrandCategoryList")
	@ResponseBody
	public JsonResult<Map<String, Object>> getBrandCategoryList(HttpServletRequest request){
		HashedMap<String, Object> resultMap = new HashedMap<String, Object>();
		BrandInfo brandInfo = new BrandInfo();
		brandInfo.setBrandStatus(BrandStatus.STATUS_1.getCode());
		BrandInfoVo bi = new BrandInfoVo();
		bi.setId(0);
		bi.setBrandName("品牌");
		List<BrandInfoVo> allBrandList = new ArrayList<BrandInfoVo>();
		allBrandList.add(bi);
		List<BrandInfoVo> brandList = brandInfoService.selectBrandList(brandInfo);
		if(brandList.size()>0) {
			allBrandList.addAll(brandList);
			for (BrandInfoVo brandInfoVo : brandList) {
				String logo = brandInfoVo.getBrandLogo();
				if(StringUtils.isNoneBlank(logo)) {
					brandInfoVo.setBrandLogo(GlobalConfig.FILE_SERVICE_LOCATION+logo);
				}
			}
		}		
		resultMap.put("allBrandList", allBrandList);
		
		ProductCategoryQuery categoryQuery = new ProductCategoryQuery();
		categoryQuery.setCategoryStatus(CategoryStatus.STATUS_1.getCode());
		List<ProductCategoryVo> categoryoList = productCategoryService.selectList(categoryQuery);
		resultMap.put("allCategoryList", categoryoList);
			
		return new JsonResult<Map<String, Object>>(resultMap, HttpCode.SUCCESS);
	}
}
